//
//  JVisitorLoginView.swift
//  JMicroBlog
//
//  Created by Jason on 18/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit
// MARK: - 代理协议
protocol JVisitorLoginViewDelegate: NSObjectProtocol {
    /** 点击登录按钮 */
    func visitorLoginViewSignInSelected()
    
    /** 点击注册按钮 */
    func visitorLoginViewSingUpSelected()
}
// MARK: 访客视图类
class JVisitorLoginView: UIView {
    
    // MARK: - 成员属性
    
    // 首页旋转的图片
    @IBOutlet  weak var smallIconView: UIImageView!
    
    // 图标内容
    @IBOutlet weak var iconView: UIImageView!
    
    // 提示内容
    @IBOutlet weak  var msgView: UILabel!
    
    // 代理属性
    weak var delegate: JVisitorLoginViewDelegate?
    
    // MARK: - 关键函数
    
    func setInfo(iconViewName:String,_ message:String,_ isHome:Bool = false) {
        
        iconView.hidden = !isHome
        
        // 是首页
        if isHome {
            iconView.image = UIImage(named: iconViewName)
            if smallIconView.layer.actionForKey("keyPath") == nil {
                startAnimation()
            }

        } else {
            smallIconView.image = UIImage(named: iconViewName)
        }
        msgView.text = message
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 10.0
        
        smallIconView.layer.addAnimation(anim, forKey: "keyPath")
        
    }
    
    // MARK: - 事件函数
    
    /** 注册 */
    @IBAction func signUpBtn() {
       delegate?.visitorLoginViewSingUpSelected()
    }
    
    /** 登录 */
    @IBAction func signInBtn() {
        delegate?.visitorLoginViewSignInSelected()
    }
    
    // MARK: 辅助函数
    
    // 类方法创建访问视图对象
    class func initWithNib(nibName : String = "JVisitorLoginView") -> JVisitorLoginView? {
        return NSBundle.mainBundle().loadNibNamed(nibName, owner: nil, options: nil).last as? JVisitorLoginView
    }
}
